//
//  HanZiXunLianViewController.swift
//  ZHM
//
//  Created by zzddongge on 16/7/21.
//  Copyright © 2016年 zzd. All rights reserved.
//

import UIKit

class HanZiXunLianViewController: UIViewController {

    //获取模拟器的宽度和高度
    let screenWidth = UIScreen.main.bounds.size.width
    let screenHeight = UIScreen.main.bounds.size.height
    var navigationHeight:CGFloat?
    var navigationHeights:CGFloat?
    var yunImageView:UIImageView?
    var xiaoJianPanXunLianBtn:UIButton?
    var biXingXunLianBtn:UIButton?
    var danZiXunLianBtn:UIButton?
    var ciZuXunLianBtn:UIButton?
    var wenZhangXunLianBtn:UIButton?
    

    override func viewDidLoad() {
        self.title = "汉字训练"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
        self.view.backgroundColor = UIColor(red: 246/255,green: 246/255,blue:246/255,alpha: 1.0)

        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
       
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        //修改导航栏按钮颜色
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.barTintColor =
            UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1)
        
        //初始化汉字训练首页顶部的imageView
        yunImageView = UIImageView(image: UIImage(named: "ZHHZJXa.02_01.L"))
        navigationHeight = self.navigationController?.navigationBar.frame.size.height
        navigationHeights = navigationHeight!+20
        yunImageView?.frame = CGRect(x: 0, y: navigationHeights!, width: screenWidth, height: screenWidth/3+20)
        
        
        //初始化五个按钮
        //初始化小键盘训练按钮
        xiaoJianPanXunLianBtn = UIButton(frame:CGRect(x: (screenWidth/2-100)/2,y: navigationHeights!+screenWidth/3+40,width: 100,height: 100))
        fengZhuangAttributes(xiaoJianPanXunLianBtn!)
        
        //初始化笔形训练按钮
        biXingXunLianBtn = UIButton(frame:CGRect(x: screenWidth/2+(screenWidth/2-100)/2,y: navigationHeights!+screenWidth/3+40,width: 100,height: 100))
        fengZhuangAttributes(biXingXunLianBtn!)
        
        //初始化文章训练按钮
        wenZhangXunLianBtn = UIButton(frame:CGRect(x: (screenWidth-120)/2,y: navigationHeights!+screenWidth/3+130,width: 120,height: 120))
        fengZhuangAttributes(wenZhangXunLianBtn!)
        
        //初始化单字训练
        danZiXunLianBtn = UIButton(frame:CGRect(x: (screenWidth/2-100)/2,y: navigationHeights!+screenWidth/3+240,width: 100,height: 100))
        fengZhuangAttributes(danZiXunLianBtn!)
        
        //初始化词组训练
        ciZuXunLianBtn = UIButton(frame:CGRect(x: screenWidth/2+(screenWidth/2-100)/2,y: navigationHeights!+screenWidth/3+240,width: 100,height: 100))
        fengZhuangAttributes(ciZuXunLianBtn!)
        
        self.view.addSubview(yunImageView!)
        self.view.addSubview(xiaoJianPanXunLianBtn!)
        self.view.addSubview(biXingXunLianBtn!)
        self.view.addSubview(wenZhangXunLianBtn!)
        self.view.addSubview(danZiXunLianBtn!)
        self.view.addSubview(ciZuXunLianBtn!)
        
        //分别给五个训练模块添加事件
        xiaoJianPanXunLianBtn?.addTarget(self, action: #selector(xiJiaoPanXunLian), for: .touchUpInside)
        biXingXunLianBtn?.addTarget(self, action: #selector(biXingXunLian), for: .touchUpInside)
        wenZhangXunLianBtn?.addTarget(self, action: #selector(wenZhangXunLian), for: .touchUpInside)
        danZiXunLianBtn?.addTarget(self, action: #selector(danZiXunLian), for: .touchUpInside)
        ciZuXunLianBtn?.addTarget(self, action: #selector(ciZuXunLian), for: .touchUpInside)
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(_ animated: Bool) {
        self.title = "汉字训练"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //封装初始化按钮的一些属性
    func fengZhuangAttributes(_ uiButton:UIButton!){
        uiButton.clipsToBounds = true
        uiButton.titleLabel?.font = UIFont(name: "Helvetica-Bold",size: 15)
        switch uiButton {
        case xiaoJianPanXunLianBtn!:
             xiaoJianPanXunLianBtn?.backgroundColor = UIColor(red: 142/255,green: 68/255,blue: 173/255,alpha: 1)
             xiaoJianPanXunLianBtn?.layer.cornerRadius = 50
             xiaoJianPanXunLianBtn?.setTitle("小键盘训练", for: UIControlState())
             break
        case biXingXunLianBtn!:
             biXingXunLianBtn?.backgroundColor = UIColor(red: 142/255,green: 68/255,blue: 173/255,alpha: 1)
             biXingXunLianBtn?.layer.cornerRadius = 50
             biXingXunLianBtn?.setTitle("笔形训练", for: UIControlState())
             break
        case wenZhangXunLianBtn!:
             wenZhangXunLianBtn?.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
             wenZhangXunLianBtn?.layer.cornerRadius = 60
             wenZhangXunLianBtn?.setTitle("文章训练", for: UIControlState())
             break
        case danZiXunLianBtn!:
             danZiXunLianBtn?.backgroundColor = UIColor(red: 243/255,green: 156/255,blue: 18/255,alpha: 1)
             danZiXunLianBtn?.layer.cornerRadius = 50
             danZiXunLianBtn?.setTitle("单字训练", for: UIControlState())
             break
        case ciZuXunLianBtn!:
             ciZuXunLianBtn?.backgroundColor = UIColor(red: 243/255,green: 156/255,blue: 18/255,alpha: 1)
             ciZuXunLianBtn?.layer.cornerRadius = 50
             ciZuXunLianBtn?.setTitle("词组训练", for: UIControlState())
            break
        default:
            print("ok")
        }
    }
    
    func xiJiaoPanXunLian(){
        let xiJianPanXunLianViewController = XiaoJianPanXunLianViewController()
        self.navigationController?.pushViewController(xiJianPanXunLianViewController, animated: true)
    
    }
    
    func biXingXunLian(){
        let fourCategoryViewController = FourCategoryViewController()
        self.navigationController?.pushViewController(fourCategoryViewController, animated: true)
    
    }

    func wenZhangXunLian(){
        let wenZhangXunLianViewController = WenZhangXunLianViewController()
        self.navigationController?.pushViewController(wenZhangXunLianViewController, animated: true)
    
    }
    
    func danZiXunLian(){
        let danZiXunLianViewController = DanZiXunLianViewController()
        self.navigationController?.pushViewController(danZiXunLianViewController, animated: true)
    
    }
    
    func ciZuXunLian(){
        let ciZuXunLianViewController = CiZuXunLianViewController()
        self.navigationController?.pushViewController(ciZuXunLianViewController, animated: true)
    
    }
    
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
